var error = 'error decoding QR Code';
var people = {};

$(document).ready(function () {
    var sayCheese = new SayCheese('#canvas',
    {
        audio: false
    });
    sayCheese.on('snapshot', function (snapshot) {
        qrcode.decode(snapshot.toDataURL());
    });
    sayCheese.on('error', function (error) {
        showInfo(error);
    });
    sayCheese.start();

    $('#in').click(function () {
        var $this = $(this);
        scan($this, function () {
            var name = getInfo();
            if (people[name] != null) {
                showMessage('Ya ingresado', 'danger');
                return;
            }
            people[name] = { consume: false };
            refreshTable();
            showMessage(name + ' Ingresado', 'success');
        });
    });

    $('#out').click(function () {
        var $this = $(this);
        scan($this, function () {
            var name = getInfo();
            if (people[name] == null) {
                showMessage('No ingresado', 'danger');
                return;
            }
            if (!people[name].consume) {
                showMessage('No consumio aun', 'warning');
                return;
            }
            delete people[name];
            refreshTable();
            showMessage('Dejar salir', 'success');
        });
    });

    $('#consume').click(function () {
        var $this = $(this);
        scan($this, function () {
            var name = getInfo();
            if (people[name] == null) {
                showMessage('No ingresado', 'danger');
                return;
            }
            people[name].consume = true;
            refreshTable();
            showMessage('Consumo registrado', 'success');
        });
    });

    $('#status').click(function () {
        var $this = $(this);
        scan($this, function () {
            var name = getInfo();
            if (people[name] == null) {
                showMessage('No ingresado', 'danger');
                return;
            }
            var msg = people[name].consume ? "Consumio" : "No Consumio";
            showMessage(msg, 'info');
        });
    });

    function scan($button, callback) {
        showInfo('Scanning...');
        showInfo('Scanning...');
        setStyle($button, 'btn-warning');
        sayCheese.takeSnapshot();

        qrcode.callback = function (data) {
            var hasError = data == error;
            setStyle($button, hasError ? 'btn-danger' : 'btn-primary');
            showInfo(data);
            if (!hasError && callback != null)
                callback();
        };
    }

    function refreshTable() {
        var body = '';
        for (var key in people) {
            if (!people.hasOwnProperty(key))
                return;
            var val = people[key];
            var style = val.consume ? 'glyphicon glyphicon-glass' : 'glyphicon glyphicon-minus';
            var status = '<span class="glyphicon ' + style + '"></span>';
            body += '<tr><td>' + key + '</td><td>' + status + '</td></tr>';
        };
        $('#people tbody').html(body);
    }
});

function showInfo(info) {
    $("#result").val(info);
};

function getInfo() {
    return $("#result").val();
};

function setStyle($btn, style) {
    $btn.attr('class', 'btn ' + style);
}

function showMessage(msg, style) {
    $('#msg').html(msg).attr('class', 'alert alert-' + style);
}